package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.CompanyEvalDao;
import ex.com.jm.food.entity.Evaluation;

public class CompanyEvalDaoImpl extends HibernateDaoSupport implements
		CompanyEvalDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Evaluation> getEvaluations(final int initPage,final int pageRow,
			final String comId,final String menuName,final Integer start,final String s_date,
			final String e_date,final String order,final String sort) {
		// TODO Auto-generated method stub
		Object evlas=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="select new ex.com.jm.food.entity.dto.EvlaDTO("
						+ "eval.evalId,"
						+ "eval.evalStar,"
						+ "eval.evalComment,"
						+ "eval.evalDate,"
						+ "menu.menuName,"
						+ "user.userName"
						+ ") from Evaluation eval "
						+ " join eval.menuId menu "
						+ " join eval.userId user"
						+ " where eval.comId='"+comId+"' ";
				if (menuName!=null&&!menuName.equals("")) {
					hql+=" and menu.menuName like '%"+menuName+"%'";
				}
				if(start!=null&&!start.equals("")){
					if(start==1){
						hql+=" and eval.evalStar=5";
					}
					if(start==2){
						hql+=" and eval.evalStar<=4 and eval.evalStar>=3";
					}if(start==3) {
						hql+=" and eval.evalStar<=2";
					}
					
				}
				if(s_date!=null&&!s_date.equals("")){
					hql+=" and eval.evalDate>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql+=" and eval.evalDate<='"+e_date+"'";
				}
				if(order!=null&&!order.equals("")&&sort!=null&&!sort.equals("")){
					hql+=" order by "+order+" "+sort;
				}else {
					hql+=" order by eval.evalDate desc";
				}
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<Evaluation>) evlas;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int evalCount(final String comId,final String menuName,final Integer start,
			final String s_date,final String e_date) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="select new ex.com.jm.food.entity.dto.EvlaDTO("
						+ "eval.evalId,"
						+ "eval.evalStar,"
						+ "eval.evalComment,"
						+ "eval.evalDate,"
						+ "menu.menuName,"
						+ "user.userName"
						+ ") from Evaluation eval "
						+ " join eval.menuId menu "
						+ " join eval.userId user"
						+ " where eval.comId='"+comId+"' ";
				if (menuName!=null&&!menuName.equals("")) {
					hql+=" and menu.menuName like '%"+menuName+"%'";
				}
				if(start!=null&&!start.equals("")){
					if(start==1){
						hql+=" and eval.evalStar=5";
					}
					if(start==2){
						hql+=" and eval.evalStar<=4 and eval.evalStar>=3";
					}if(start==3) {
						hql+=" and eval.evalStar<=2";
					}
					
				}
				if(s_date!=null&&!s_date.equals("")){
					hql+=" and eval.evalDate>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql+=" and eval.evalDate<='"+e_date+"'";
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

}
